local wanwei = fk.CreateSkill {

  name = "joy__wanwei",

  tags = {  },

}



wanwei:addEffect(fk.EnterDying, {
  name = "joy__wanwei",
  anim_type = "support",
  events = {fk.EnterDying, fk.AfterDying},
  can_trigger = function(self, event, target, player, data)
    if event == fk.EnterDying then
      return player:hasSkill(wanwei.name) and target ~= player and not player:isNude() and
        player:usedSkillTimes(wanwei.name, Player.HistoryRound) == 0
    else
      return not target.dead and data.extra_data and data.extra_data.ofl__fuding and data.extra_data.ofl__fuding[1] == player.id and
        not player.dead
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.EnterDying then
      local cards = player.room:askForCard(player, 1, 5, true, wanwei.name, true, ".", "#joy__wanwei-invoke::"..target.id)
      if #cards > 0 then
        event:setCostData(self, cards)
        return true
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EnterDying then
      room:moveCardTo(event:getCostData(self), Card.PlayerHand, target, fk.ReasonGive, wanwei.name)
      data.extra_data = data.extra_data or {}
      data.extra_data.ofl__fuding = {player.id, #event:getCostData(self)}
    else
      player:drawCards(data.extra_data.ofl__fuding[2], wanwei.name)
      if player:isWounded() and not player.dead then
        room:recover{
          who = player,
          num = 1,
          recoverBy = player,
          skillName = wanwei.name
        }
      end
    end
  end,
})
wanwei:addEffect(fk.AfterDying, {
  name = "joy__wanwei",
  anim_type = "support",
  events = {fk.EnterDying, fk.AfterDying},
  can_trigger = function(self, event, target, player, data)
    if event == fk.EnterDying then
      return player:hasSkill(wanwei.name) and target ~= player and not player:isNude() and
        player:usedSkillTimes(wanwei.name, Player.HistoryRound) == 0
    else
      return not target.dead and data.extra_data and data.extra_data.ofl__fuding and data.extra_data.ofl__fuding[1] == player.id and
        not player.dead
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.EnterDying then
      local cards = player.room:askForCard(player, 1, 5, true, wanwei.name, true, ".", "#joy__wanwei-invoke::"..target.id)
      if #cards > 0 then
        event:setCostData(self, cards)
        return true
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EnterDying then
      room:moveCardTo(event:getCostData(self), Card.PlayerHand, target, fk.ReasonGive, wanwei.name)
      data.extra_data = data.extra_data or {}
      data.extra_data.ofl__fuding = {player.id, #event:getCostData(self)}
    else
      player:drawCards(data.extra_data.ofl__fuding[2], wanwei.name)
      if player:isWounded() and not player.dead then
        room:recover{
          who = player,
          num = 1,
          recoverBy = player,
          skillName = wanwei.name
        }
      end
    end
  end,
})

return wanwei